package cn.niit.service.impl;

import cn.niit.dao.CourseTimeDao;
import cn.niit.entity.CourseTime;
import cn.niit.service.CourseTimeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 上课时间表(CourseTime)表服务实现类
 *
 * @author makejava
 * @since 2021-11-14 16:06:44
 */
@Service("courseTimeService")
public class CourseTimeServiceImpl implements CourseTimeService {
    @Resource
    private CourseTimeDao courseTimeDao;

    @Override
    public CourseTime queryByCourseTime(Integer section, Integer week) {
        
        return courseTimeDao.queryByCourseTime(section, week);
    }

    @Override
    public List<CourseTime> queryByClassCode(CourseTime courseTime) {
        return courseTimeDao.queryByClassCode(courseTime);
    }

    @Override
    public List<CourseTime> queryByCourseTimeList(CourseTime courseTime) {
        //固定的课程表模板集合
        List<CourseTime> courseTimeList = new ArrayList<>();
        //数据库中查到的集合
        List<CourseTime> tempList = courseTimeDao.select(courseTime);
        //循环节次
        for (int sec = 1; sec < 6; sec++) {
            //循环星期
            for (int week = 1; week < 8; week++) {
                //单元格是否有值  是：true  否：false
                boolean hasFlag = false;
                for (CourseTime time : tempList) {
                    int csSec = time.getSection();
                    int csWeek = time.getWeek();
                    if (sec == csSec && week == csWeek) {
                        courseTimeList.add(time);
                        hasFlag = true;
                        break;
                    }
                }
                if (!hasFlag) {
                    CourseTime cs = new CourseTime();
                    cs.setSection(sec);
                    cs.setWeek(week);
                    cs.setClassCode(courseTime.getClassCode());
                    cs.setId(0);
                    courseTimeList.add(cs);
                }
            }
        }

        return courseTimeList;
    }

    @Override
    public List<CourseTime> select(CourseTime courseTime) {

        return courseTimeDao.select(courseTime);
    }

    @Override
    public List<CourseTime> queryByCourseCode(CourseTime courseTime) {
        return courseTimeDao.queryByCourseCode(courseTime);
    }

    @Override
    public CourseTime queryCourseTimeById(Integer id) {
        return courseTimeDao.queryCourseTimeById(id);
    }

    @Override
    public boolean addCourseTimeList(CourseTime courseTime) {
        int i = courseTimeDao.addCourseTimeList(courseTime);
        return i > 0;
    }

    @Override
    public boolean editCourseTimeList(CourseTime courseTime) {
        int i = courseTimeDao.editCourseTimeList(courseTime);
        return i > 0;
    }

    @Override
    public boolean deleteCourseTimeListById(Integer id) {
        int i = courseTimeDao.deleteCourseTimeListById(id);
        return i > 0;
    }

    @Override
    public List<CourseTime> queryAllCourseTime() {
        return courseTimeDao.queryByCourseCode(null);
    }
}